//
//  SettingsViewController.m
//  ProjectA
//
//  Created by dllo on 16/1/24.
//  Copyright © 2016年 刘旺辉. All rights reserved.
//

#import "SettingsViewController.h"  //C
#import "ControllerForCollect.h"

#import "CellForSettings.h" //V

@interface SettingsViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, assign) float cacheSize;
@end

@implementation SettingsViewController
- (void)dealloc {
    
    self.tableView.dataSource = nil;
    self.tableView.delegate = nil;
    
    [_tableView release];
    [super dealloc];
    
}
- (void)setLightDayModel {
    [super setLightDayModel];
    
    self.barBackgroundView.backgroundColor = RGBA(44, 211, 209, 1);
    [self.tableView reloadData];
}

- (void)setNightDayModel {
    [super setNightDayModel];
    
    self.barBackgroundView.backgroundColor = RGBA(52, 52, 52, 1);
    [self.tableView reloadData];
}
- (void)viewWillAppear:(BOOL)animated {
    
    if (ISNIGHT) {
        [self setNightDayModel];
    } else {
        [self setLightDayModel];
    }
}
- (void)viewWillDisappear:(BOOL)animated {
    self.barBackgroundView.backgroundColor = L_COLOR_CLEAR;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self settingsNavigationController];
    [self createTableView];
    
}

- (void)settingsNavigationController {
    
    self.navigationItem.title = @"设置";
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"iconfont-fanhui(heise)"] style:UIBarButtonItemStylePlain target:self action:@selector(houtuiAction:)];
}



- (void)createTableView {
   
    self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    self.tableView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.tableView];
    [_tableView release];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.bounces = NO;
    //self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;    //设置分割线

    self.tableView.tableFooterView = [[[UIView alloc] init] autorelease];
    
    [self.tableView registerClass:[CellForSettings class] forCellReuseIdentifier:@"pool1"];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"pool2"];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    switch (indexPath.row) {
        case 0: {
            
            CellForSettings *cell = [tableView dequeueReusableCellWithIdentifier:@"pool1"];
            cell.backgroundColor = [UIColor clearColor];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            return cell;
            
        }
        case 1: {
            UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"pool2"];
            cell.backgroundColor = [UIColor clearColor];
            cell.textLabel.text = @"清除缓存";
            cell.imageView.image = [UIImage imageNamed:@"iconfont-lajixiang"];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            
            /** 获取缓存大小 */
            NSUInteger size = [[SDImageCache sharedImageCache] getSize];
            float cacheSize = size / 1024.0 / 1024.0;
            
            cell.detailTextLabel.text = [NSString stringWithFormat:@"%.2fM", cacheSize];
            [UIView setColorWithLabelText:cell.textLabel nightColor:L_COLOR_WHITE dayColor:L_COLOR_BLACK];
            [UIView setColorWithLabelText:cell.detailTextLabel nightColor:L_COLOR_WHITE dayColor:L_COLOR_BLACK];
            return cell;
        }
        case 2: {
            UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"pool2"];
            cell.backgroundColor = [UIColor clearColor];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.textLabel.text = @"我的收藏";
            cell.imageView.image = [UIImage imageNamed:@"iconfont-wodeshoucang"];
            [UIView setColorWithLabelText:cell.textLabel nightColor:L_COLOR_WHITE dayColor:L_COLOR_BLACK];
            return cell;
        }
        
    }
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (1 == indexPath.row) {
        /** 获取缓存大小 */
        NSUInteger size = [[SDImageCache sharedImageCache] getSize];
        float cacheSize = size / 1024.0 / 1024.0;
    
        NSString *string = [NSString stringWithFormat:@"清除:%.2fM",cacheSize];
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:string preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"否" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            //不作处理
        }];
        
        UIAlertAction *OK = [UIAlertAction actionWithTitle:@"是" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            //清除缓存
            [[SDImageCache sharedImageCache] clearDisk];
            [self.tableView reloadData];
            
        }];
        
        [alert addAction:cancel];
        [alert addAction:OK];
        [self presentViewController:alert animated:YES completion:nil];
    } else if (2 == indexPath.row) {
        ControllerForCollect *collect = [[ControllerForCollect alloc] init];
        self.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:collect animated:YES];
        [collect release];
        self.hidesBottomBarWhenPushed = NO;
    }
    
}


- (void)houtuiAction:(UIBarButtonItem *)item {
    [self.navigationController popViewControllerAnimated:YES];
    self.tabBarController.tabBar.hidden = NO;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
